package com.gemframework.oauth.impl;
import com.gemframework.model.enums.WhetherEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import weixin.popular.api.SnsAPI;
import weixin.popular.bean.sns.SnsToken;
import weixin.popular.bean.user.User;

import static com.gemframework.common.constant.GemConstant.Lang.CN;

@Slf4j
@Service
public class OauthWechatServiceImpl{

    private static final String WECHAT_MP_APPID = "wx6d7165cf1220d3f0";
    private static final String WECHAT_MP_SECRET = "e687d08c1fef5bf97f030b0cf24525c2";

    private static final String REDIRECT_URI = "http://fs.yxsdcti.com/admin/oauth/wechat/callback";

    /**
     * Oauth鉴权
     * @return
     */
    public String authorizeUri() {
        String param = "";
        return SnsAPI.connectOauth2Authorize(WECHAT_MP_APPID,REDIRECT_URI,true,param);
    }

    /**
     * 获取微信accessToken
     * @param code
     * @return
     */
    public SnsToken accessToken(String code) {
        SnsToken snsToken = SnsAPI.oauth2AccessToken(WECHAT_MP_APPID,WECHAT_MP_SECRET,code);
        return snsToken;
    }

    /**
     * 返回微信用户信息
     * @param accessToken
     * @param openId
     * @return
     */
    public User userInfo(String accessToken, String openId) {
        User user = SnsAPI.userinfo(accessToken,openId, CN, WhetherEnum.YES.getCode());
        return user;
    }
}